package com.cait.dutyfree.action.admin;

import com.cait.dutyfree.pojo.LoginUser;
import com.cait.dutyfree.security.Validator;
import com.opensymphony.xwork2.ActionSupport;

public class AddLoginUserAction extends ActionSupport {
	private LoginUser loginUser = new LoginUser();

	public LoginUser getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(LoginUser loginUser) {
		this.loginUser = loginUser;
	}

	@Override
	public String execute() throws Exception {
		this.validateSubmitAddUser();
		if (this.hasErrors()) {
			return INPUT;
		} else {
			return SUCCESS;
		}
	}

	public void validateSubmitAddUser() {
		if (this.loginUser.getfName() == null
				|| this.loginUser.getfName().isEmpty()
				|| this.loginUser.getfName().trim().length() < 2) {
			this.addFieldError("loginUser.fName",
					"First name should be more than 2 charactors");
		}
		if (!Validator.isValidPassword(loginUser.getUsername(),
				loginUser.getPassword(), loginUser.getcPassword())) {
			addFieldError("password", "Passwords must be more than 8 letters");
			addFieldError("password", "Passwords is not containing a number");
			addFieldError("password",
					"Password should not contain the username");
			addFieldError("password",
					"Passwords is not containing a special char");
			addFieldError("password", "Passwords and confirmation are not same");
		}
		if (!Validator.isValidEmailAddress(loginUser.getEmail())) {
			addFieldError("email", "Invalid email address");
		}
	}

	public String getAddLoginUser() {
		return SUCCESS;
	}

}
